package com.cgw.dmplt.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class BaseDomain {
	/**
	 * BATCH_SIZE
	 */
	public static final int BATCH_SIZE = 5000;

	/** 最大行数. */
	private static final int MAX_ROWS = 9999999;

	
	private int pageSize=2;
	/**
	 * 动态字段. 在ibatis文件中可用“dynamicFields.xxx”方式读取动态字段值
	 */
	protected Map dynamicFields = new HashMap();

	/** 起始行数（oracle物理行号从1开始）. */
	private int start = 0;

	/**
	 * 结束行数（如果不设置结束行，缺省查所有满足条件记录）.
	 */
	private int end = MAX_ROWS;

	/**
	 * 排序字段(例sp.spCode).
	 */
	private String sort;

	/**
	 * 正序|反序(例ASC).
	 */
	private String order;

	
	private Integer curentPage=1;
	
	public Integer getCurentPage() {
		return this.curentPage;
	}

	public void setCurentPage(Integer curentPage) {
		this.curentPage = curentPage;
	}

	/**
	 * 设置动态字段值.
	 * 
	 * @param fieldName
	 *            字段名称
	 * @param value
	 *            字段值
	 */
	public void setField(String fieldName, Object value) {
		dynamicFields.put(fieldName, value);
	}

	/**
	 * 返回动态字段值.
	 * 
	 * @param fieldName
	 *            字段名称
	 * @return 对象
	 */
	public Object getField(String fieldName) {
		if (dynamicFields == null) {
			return null;
		}
		return getDynamicFields().get(fieldName);
	}

	/**
	 * 返回字典编码值.
	 * 
	 * @param type
	 *            分类
	 * @param key
	 *            字典编码
	 * @return 字典编码值
	 */
	// public String getDict(String codeType, String key) {
	// // 从数据字典容器得到相应DO
	// CodeDict codeDict = CodeDictContext.getContext().getCodeDict(codeType,
	// key);
	// if (codeDict != null) {
	// return codeDict.getValue();
	// } else {
	// return null;
	// }
	// }


	@Override 
	public String toString() { 
		 return  ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getStart() {
		if(curentPage==null || curentPage.intValue()==0){
			curentPage=1;
		}
		start=(curentPage.intValue()-1)*pageSize;
		
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public Map getDynamicFields() {
		if (dynamicFields != null && dynamicFields.size() > 0) {
			Set set = dynamicFields.keySet();
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				if (dynamicFields.get(key) != null
						&& dynamicFields.get(key).getClass().isArray()) {
					Object[] objArr = (Object[]) dynamicFields.get(key);
					if (objArr.length == 1) {
						dynamicFields.put(key, ((Object[]) dynamicFields
								.get(key))[0]);
					}
				}
			}
		}
		return dynamicFields;
	}

	public void setDynamicFields(Map dynamicFields) {
		this.dynamicFields = dynamicFields;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	/**
	 * clear object properties
	 * 
	 * @param clazz
	 * @param nullInstanse
	 */
	@SuppressWarnings( { "rawtypes" })
	public static void clear(Class clazz, Object nullInstanse) {
		if (clazz != null && nullInstanse != null) {
			Method[] mds = clazz.getDeclaredMethods();
			for (Method method : mds) {
				if (method.getName().startsWith("set")) {
					try {
						method.invoke(nullInstanse, new Object[] { null });
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * clone one object
	 * 
	 * @param orig
	 * @return cloneObj
	 */
	public static Object depthClone(Object orig) {
		Object cloneObj = null;
		try {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ObjectOutputStream oo = new ObjectOutputStream(out);
			oo.writeObject(orig);

			ByteArrayInputStream in = new ByteArrayInputStream(out
					.toByteArray());
			ObjectInputStream oi = new ObjectInputStream(in);
			cloneObj = oi.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return cloneObj;
	}
	
	
	
	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
}
